package com.casic.ops.runner.pipeline.manager;

import com.casic.ops.configs.ApplicationContextUtil;
import com.casic.ops.entity.domain.MachineInfoDO;
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.Session;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * channel缓存管理 //已经抛弃了该ssh连接方式
 * @author cww
 * @date 2022/6/17
 */
@Slf4j
@Service
public class ChannelManager {

    //暂时不清楚什么bug，SeeionManager和mySessionManager明明注入进来了，但到实际使用get()时爆空指针，先手动new一个
//    @Resource
//    private SessionManager sessionManager;
//
//    @Resource
//    private MySessionManager mySessionManager;

    @SneakyThrows
    public ChannelShell getChannelShell(MachineInfoDO machineInfoDO){
        SessionManager sessionManager= ApplicationContextUtil.getBean(SessionManager.class);
        sessionManager.setMachineInfoDO(machineInfoDO);
        Session session = sessionManager.get();
        return (ChannelShell) session.openChannel("shell");
    }

    @SneakyThrows
    public ChannelShell getmyChannelShell(){
        MySessionManager mySessionManager =ApplicationContextUtil.getBean(MySessionManager.class);
        Session session = mySessionManager.get();
        return (ChannelShell) session.openChannel("shell");
    }
}
